package cn.chengpengper.seckill.order.interfaces.controller;

import cn.chengpengper.seckill.common.code.ErrorCode;
import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.model.dto.SeckillOrderSubmitDTO;
import cn.chengpengper.seckill.common.response.ResponseMessage;
import cn.chengpengper.seckill.common.response.ResponseMessageBuilder;
import cn.chengpengper.seckill.order.application.model.command.SeckillOrderCommand;
import cn.chengpengper.seckill.order.application.model.command.SeckillOrderTaskCommand;
import cn.chengpengper.seckill.order.application.service.SeckillOrderService;
import cn.chengpengper.seckill.order.application.service.SeckillSubmitOrderService;
import cn.chengpengper.seckill.order.domain.model.entity.SeckillOrder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chengpeng.hu
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", originPatterns = "*")
public class SeckillOrderController {

    private final SeckillOrderService seckillOrderService;

    private final SeckillSubmitOrderService seckillSubmitOrderService;

    @GetMapping("/v1/getSeckillOrderByGoodsId")
    public ResponseMessage<List<SeckillOrder>> getSeckillOrderByGoodsId(@RequestParam Long goodsId){
        List<SeckillOrder> seckillOrderList = seckillOrderService.getSeckillOrderByGoodsId(goodsId);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillOrderList);
    }

    /**
     * 保存秒杀订单,异步化下单
     *
     * @param seckillOrderCommand 秒杀订单实体
     */
    @PostMapping("/v3/saveSeckillOrder")
    public ResponseMessage<SeckillOrderSubmitDTO> saveSeckillOrderV2(@RequestAttribute Long userId, @RequestBody SeckillOrderCommand seckillOrderCommand) {
        SeckillOrderSubmitDTO res = seckillSubmitOrderService.saveSeckillOrder(userId, seckillOrderCommand);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), res);
    }

    /**
     * 根据用户ID获取订单列表
     *
     * @param userId 用户ID
     * @return 订单列表
     */
    @GetMapping("/v1/getSeckillOrderByUserId")
    public ResponseMessage<List<SeckillOrder>> getSeckillOrderByUserId(@RequestParam Long userId) {
        List<SeckillOrder> seckillOrderList = seckillOrderService.getSeckillOrderByUserId(userId);
        return ResponseMessageBuilder.build(HttpCode.SUCCESS.getCode(), seckillOrderList);
    }

    /**
     * 异步下单时，会返回任务id，如果下单返回的是任务id，则调用此接口获取订单id，如果前端多次调用此接口返回的数据仍为为任务id，则表示下单失败，不再调用
     * 如果调用此接口（含重试），返回了订单id，则异步下单获取到了与同步下单相同的数据，就可以通过订单id来查询订单信息了。
     */
    @RequestMapping(value = "/getSeckillOrderSubmitDTO", method = {RequestMethod.GET,RequestMethod.POST})
    public ResponseMessage<SeckillOrderSubmitDTO> get(@RequestAttribute Long userId, SeckillOrderTaskCommand seckillOrderTaskCommand){
        SeckillOrderSubmitDTO seckillOrderSubmitDTO = seckillOrderService.getSeckillOrderSubmitDTOByTaskId(seckillOrderTaskCommand.getOrderTaskId(), userId, seckillOrderTaskCommand.getGoodsId());
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillOrderSubmitDTO);
    }
}
